package com.example.zzl.adapter.HomeFragment;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.example.zzl.R;
import com.example.zzl.adapter.TwoMyBaseAdapter;
import com.example.zzl.databinding.HomeSpecialInfoItemBinding;
import com.example.zzl.model.bean.InfoFoot;
import com.example.zzl.utils.specification.VolleySingleton;

import java.util.List;

/**
 * Created by ligg on 2017/2/15.
 */

public class InfoFootAdapter extends TwoMyBaseAdapter<InfoFoot, InfoFootAdapter.ViewHolder>{


    public InfoFootAdapter(Context context, List<InfoFoot> list) {
        super(context, list);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        HomeSpecialInfoItemBinding infoDb = DataBindingUtil.inflate(inflater, R.layout.home_special_info_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(infoDb);
        viewHolder.infoDb = infoDb;
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        InfoFoot foot = list.get(position);
        holder.infoDb.setFoot(foot);
        holder.infoDb.executePendingBindings();
        final ImageView iv = holder.infoDb.iv;
        iv.setImageBitmap(null);
        iv.setTag(foot.MainImage);
        VolleySingleton.volleySingleton.getImageLoader().get(foot.MainImage, new ImageLoader.ImageListener() {
            @Override
            public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                if (iv.getTag().equals(response.getRequestUrl())) {
                    iv.setImageBitmap(response.getBitmap());
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    class ViewHolder extends TwoMyBaseAdapter.BaseHolder {

        private HomeSpecialInfoItemBinding infoDb;

        public ViewHolder(ViewDataBinding db) {
            super(db);
        }
    }
}
